// pages/person-information/index.js
const db = wx.cloud.database()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        clientHeight: 557,
        indexSex: 0,
        indexPolitic: 0,
        indexNotion: 0,
        picker: ['男', '女'],
        pickerPolitic: ['群众', '中共党员', '中共预备党员', '共青团员', '民革党员', '民盟盟员', '民键会员', '民进领', '农工党党员', '致公党党员', '九三学社社员', '台盟盟员', '无党派人士'],
        pickerNotion: ['汉族', '蒙古族', '回族', '藏族', '维吾尔族', '苗族', '彝族', '壮族', '布依族', '朝鲜族', '满族', '侗族', '瑶族', '白族', '土家族', '哈尼族', '哈萨克族', '傣族', '黎族', '僳僳族', '佤族', '畲族', '高山族', '拉祜族', '水族', '东乡族', '纳西族', '景颇族', '柯尔克孜族', '土族', '达斡尔族', '仫佬族', '羌族', '布朗族', '撒拉族', '毛南族', '仡佬族', '锡伯族', '阿昌族', '普米族', '塔吉克族', '怒族', '乌孜别克族', '俄罗斯族', '鄂温克族', '德昂族', '保安族', '裕固族', '京族塔', '塔尔族', '独龙族鄂伦春族', '赫哲族', '门巴族', '珞巴族', '基诺族'],
        date: '2018-12-25',
        username: null,
        email: null,
        name: null,
        phone: null,
        phone_guding: null,
        qqNum: null,
        wxNum: null
    },
    // wx 
    wxInput(e) {
        this.setData({
            wxNum: e.detail.value
        })
    },
    // qq
    qqInput(e) {
        this.setData({
            qqNum: e.detail.value
        })
    },
    // phone_guding
    phoneGudingInput(e) {
        this.setData({
            phone_guding: e.detail.value
        })
    },
    // phone
    phoneInput(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    // name
    nameInput(e) {
        this.setData({
            name: e.detail.value
        })
    },
    // email
    emailInput(e) {
        this.setData({
            email: e.detail.value
        })
    },
    // username
    usernameInput(e) {
        this.setData({
            username: e.detail.value
        })
    },
    cancel() {
        wx.navigateBack({
            delta: 1,
        })
    },
    // 上报
    save() {
        // console.log('用户名', this.data.username === null)
        // console.log('电子邮箱', this.data.email === null)
        // console.log('真实姓名', this.data.name === null)
        // console.log('手机', this.data.phone === null)
        // console.log('固定电话', this.data.phone_guding === null)
        // console.log('QQ号', this.data.qqNum === null)
        // console.log('微信号', this.data.wxNum === null)
        
        var that = this
        // TODO 没有实现修改username判重
        if (this.data.username != null && this.data.phone != null) {
            db.collection('user').where({
                username: this.data.username
            }).update({
                data: {
                    "username": this.data.username,
                    "name": this.data.name,
                    "sex": this.data.picker[this.data.indexSex],
                    "birth": this.data.date,
                    "email": this.data.email,
                    "politic": this.data.pickerPolitic[this.data.indexPolitic],
                    "notion": this.data.pickerNotion[this.data.indexNotion],
                    "phone": this.data.phone,
                    "phone_guding": this.data.phone_guding,
                    "qqNum": this.data.qqNum,
                    "wxNum": this.data.wxNum,
                    "indexSex": this.data.indexSex,
                    "indexPolitic": this.data.indexPolitic,
                    "indexNotion": this.data.indexNotion,
                    "date": this.data.date
                },
                success: function (res) {
                    console.log('保存修改返回数据', res)
                    // 保存修改后更新 全局 userinfo
                    db.collection('user').where({
                        username: that.data.username
                    }).get().then(res => {
                        console.log('查询user',res)
                        var app = getApp()
                        app.globalData.userinfo = res.data[0] // 个人信息放在 app.globalData.userinfo 中
                        // console.log(app.globalData)
                        wx.showToast({
                            title: '保存成功',
                            icon: 'success'
                        })
                        setTimeout(() => {
                            wx.navigateBack({
                                delta: 1
                            })
                        }, 1000);
                    })

                }
            })

        } else {
            wx.showToast({
                title: '必填内容未填写',
                icon: 'error'
            })
        }


    },
    DateChange(e) {
        this.setData({
            date: e.detail.value
        })
    },
    PickerChange(e) {
        // console.log(e);
        this.setData({
            indexSex: e.detail.value
        })
    },
    PickerChangePolitic(e) {
        this.setData({
            indexPolitic: e.detail.value
        })
    },
    PickerChangeNation(e) {
        this.setData({
            indexNotion: e.detail.value
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this;
        // 动态获取scroll-view 高度
        wx.getSystemInfo({
            success: function (res) {
                let screenHeight = wx.getSystemInfoSync().windowHeight
                // console.log(screenHeight)
                let query = wx.createSelectorQuery().in(that);
                query.select('.footer_button').boundingClientRect();
                query.select('.head').boundingClientRect();
                query.exec(res => {
                    // console.log(res)
                    let h = res[0].height;
                    let h1 = res[1].height;
                    // console.log(screenHeight - h - h1)
                    that.setData({
                        clientHeight: screenHeight - h - h1
                    })
                })

            }
        })
        // 获取用户信息
        var app = getApp()
        // console.log(app.globalData.userinfo)
        let userinfo = app.globalData.userinfo // 不存在就是 undefined
        // console.log('固定电话', userinfo.phone_guding)
        this.setData({
            username: userinfo.username,
            name: userinfo.name,
            phone: userinfo.phone,
            indexSex: userinfo.sex === '男' ? 0 : 1,
            date: userinfo.date,
            email: userinfo.email === undefined ? null : userinfo.email,
            phone_guding: userinfo.phone_guding === undefined ? null : userinfo.phone_guding,
            qqNum: userinfo.qqNum === undefined ? null : userinfo.qqNum,
            wxNum: userinfo.wxNum === undefined ? null : userinfo.wxNum,
            indexPolitic: userinfo.indexPolitic === undefined ? 0 : userinfo.indexPolitic,
            indexNotion: userinfo.indexNotion === undefined ? 0 : userinfo.indexNotion
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})